﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_BLL;
using V5_Model;
using System.IO;

namespace V5_EShop.Admin
{
    public partial class Admin_ProductField : BasicPage
    {
        protected int FClassID = 0;
        protected string strWhere = "";
        protected const string vWhere = "vWhere";
        protected void Page_Load(object sender, EventArgs e)
        {
            FClassID = Input.SetInt(Request["fclass"]);
            if (FClassID > 0)
            {
                ViewState[vWhere] = " fClassID=" + FClassID;
            }
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]).Trim().ToLower();
                Type = Input.SetString(Request["Type"]).Trim().ToLower();
                Bind_TabSelect(Type);
                switch (Action)
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            if (ViewState[vWhere] != null)
            {
                strWhere = Input.SetString(ViewState[vWhere]);
            }

            this.gvList.DataSource = new BLLProductField().GetList(strWhere);
            this.gvList.DataBind();
        }

        private void Bind_TabSelect(string type)
        {
            switch (type)
            {
                case "ispassed":
                    TabSelect2 = "select";
                    break;
                case "nopassed":
                    TabSelect3 = "select";
                    break;
                case "isalert":
                    TabSelect4 = "select";
                    break;
                default:
                    TabSelect1 = "select";
                    break;
            }
        }

        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelProductField model = new BLLProductField().GetModel(id);
            if (model != null)
            {
                this.txtFieldName.Text = model.FieldName;
                if (model.IsUseImg == 1)
                {
                    this.txtFieldValues.Text = "";
                }
                else
                {
                    this.txtFieldValues.Text = model.FieldValues;
                }
                this.hfID.Value = model.ID.ToString();
                this.chkIsUseImg.Checked = model.IsUseImg == 1 ? true : false;

                this.lblTitle.Text = "修改属性";
                this.btnSave.Text = "修改保存";
                if (model.FieldPics != "")
                {
                    this.Bind_AlreadyImgs(model.FieldValues, model.FieldPics);
                }
            }
        }

        private void Bind_Delete(int id)
        {
            new BLLProductField().Delete(id);
            this.Bind_gvList();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = Input.SetString(e.CommandName);
            int id = Input.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim())
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        //绑定属性值
        private void Bind_AlreadyImgs(string fieldValues, string productFieldImgs)
        {
            if (ViewState["AlreadyImgs"] == null)
            {
                if (productFieldImgs != "")
                {
                    string[] strArray = fieldValues.Split(new char[] { ',' });
                    string[] strArray2 = productFieldImgs.Split(new char[] { ',' });
                    DataTable table = new DataTable();
                    table.Columns.Add(new DataColumn("id"));
                    table.Columns.Add(new DataColumn("FieldValues", typeof(string)));
                    table.Columns.Add(new DataColumn("FieldImgs", typeof(string)));
                    int n = 1;
                    for (int i = 0; i < strArray2.Length; i++)
                    {
                        DataRow row = table.NewRow();
                        row["id"] = n;
                        row["FieldValues"] = strArray[i];
                        row["FieldImgs"] = strArray2[i];
                        table.Rows.Add(row);
                        n++;
                    }
                    ViewState["AlreadyImgs"] = table;
                    this.dlAlreadyImgs.DataSource = table;
                    this.dlAlreadyImgs.DataBind();
                }
            }
            else
            {
                DataTable dt = (DataTable)ViewState["AlreadyImgs"];
                this.dlAlreadyImgs.DataSource = dt;
                this.dlAlreadyImgs.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ModelProductField model = new ModelProductField();
            BLLProductField bll = new BLLProductField();
            int id = Input.SetInt(this.hfID.Value);
            int iFClassID = FClassID;
            int isUseimg = 0;
            string FieldName = this.txtFieldName.Text;
            string FieldValues = this.txtFieldValues.Text;
            int choice = this.chkChoice.Checked ? 1 : 0;
            string FieldPics = "";

            bool flag = this.chkIsUseImg.Checked;
            string FieldValues1 = "", FieldPics1 = "";
            string[] strArray = base.Request.Form["txtFieldValue"].Split(new char[] { ',' });
            if (!flag)
            {
                FieldValues = this.txtFieldValues.Text;//
            }
            else
            {
                isUseimg = 1;
                HttpFileCollection files = HttpContext.Current.Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    if (files[i].ContentLength > 0)
                    {
                        if ((Path.GetExtension(file.FileName).ToLower() == ".gif") || (Path.GetExtension(file.FileName).ToLower() == ".jpg") || (Path.GetExtension(file.FileName).ToLower() == ".png"))
                        {
                            if (!(strArray[i] == ""))
                            {
                                FieldValues1 += strArray[i] + ",";
                            }
                            else
                            {
                                Js.Alert("预设值不能为空!");
                                return;
                            }
                        }
                        else
                        {
                            Js.Alert("图片格式不对!");
                            return;
                        }
                    }
                }
                //上传图片
                for (int j = 0; j < files.Count; j++)
                {
                    HttpPostedFile file2 = files[j];
                    if (files[j].ContentLength > 0)
                    {
                        string str4 = "mm" + DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + j.ToString() + Path.GetExtension(file2.FileName).ToLower();
                        FieldPics1 += str4 + ",";
                        file2.SaveAs(Server.MapPath("~/UploadFiles/productFiled/") + str4);
                    }
                }
                //过滤字符
                if (FieldValues1.Length > 0 && FieldPics1.Length > 0)
                {
                    FieldValues = FieldValues1.Remove(FieldValues1.Length - 1);
                    FieldPics = FieldPics1.Remove(FieldPics1.Length - 1);
                }
            }
            model.FClassID = FClassID;
            model.FieldName = FieldName;
            model.FieldValues = FieldValues;
            model.FieldPics = FieldPics;
            model.IsUseImg = isUseimg;
            model.Choice = choice;
            if (id == 0)
            {
                bll.Add(model);
            }
            else if (id > 0)
            {
                ModelProductField model1 = new BLLProductField().GetModel(id);
                if (model1.IsUseImg == 1 && flag)
                {
                    if (FieldValues != "")
                    {
                        if (model1.FieldPics != "")
                        {
                            FieldValues = model1.FieldValues + "," + FieldValues;
                            FieldPics = model1.FieldPics + "," + FieldPics;
                        }
                    }
                    else
                    {
                        FieldValues = model1.FieldValues;
                        FieldPics = model1.FieldPics;
                    }
                }
                else if ((model1.IsUseImg == 1 && !flag) && (model1.FieldPics != ""))
                //如果是字段的话
                {
                    string[] strArray2 = model1.FieldPics.Split(new char[] { ',' });
                    try
                    {
                        for (int k = 0; k < strArray2.Length; k++)
                        {
                            System.IO.File.Delete(base.Server.MapPath("~/UploadFiles/productFiled/" + strArray2[k].ToString()));
                        }
                    }
                    catch
                    {
                    }
                }
                model.FieldPics = FieldPics;
                model.FieldValues = FieldValues;
                model.ID = id;
                bll.Update(model);
            }
            this.Bind_gvList();
        }

        protected void dlAlreadyImgs_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string sid = Input.SetString(this.dlAlreadyImgs.DataKeys[e.Item.ItemIndex]);
                DataTable dt = (DataTable)ViewState["AlreadyImgs"];
                if (dt != null)
                {
                    DataRow[] rowarr = dt.Select(" id=" + sid);
                    foreach (DataRow item in rowarr)
                    {
                        dt.Rows.Remove(item);
                    }
                    dt.AcceptChanges();
                    ViewState["AlreadyImgs"] = dt;
                }
                this.Bind_AlreadyImgs("", "");
            }
        }
    }
}
